<?php

namespace App\Console\Commands;

use App\Config\QueueConfig;
use App\Http\Library\Queue;
use App\Http\Model\Console\DwModel;
use Illuminate\Console\Command;

class Dw extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'job:dw';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '数仓中间库-同步任务检查';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        /*需要处理的业务线-分校数据库*/
        $list = DwModel::listDw();
        /*查询最后一次处理日期*/
        $maxDate = DwModel::maxDate();
        foreach ($list as $val) {
            /*查询数仓已经处理完毕,可以使用的表*/
            $lock = DwModel::list($val->db,$maxDate);
            if (empty($lock)) {
                continue;
            }
            foreach ($lock as $v) {
                /*查询当前表数据处理状态*/
                if (DwModel::status($v, $val->db) === true) {
                    /*注册任务*/
                    Queue::rpush([
                        'db'                  => $val->db,
                        'table'               => $v->table_name,
                        /*任务所属日期*/
                        'data_date'           => $v->data_date,
                        /*描述*/
                        'desc'                => $val->desc,
                    ], QueueConfig::DW);
                }
            }
        }
        return 0;
    }
}
